package google.sites.model.commands.site;

import com.google.gdata.data.Link;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.sites.SiteEntry;
import com.google.gdata.data.sites.SitesLink;
import com.google.gdata.util.ServiceException;
import dev.tools.design.patterns.command.Command;
import dev.tools.design.patterns.command.CommandException;
import google.sites.model.Model;
import google.sites.model.commands.SitesCommand;

import java.io.IOException;
import java.net.URL;

/**
 * Created with IntelliJ IDEA.
 * User: Nuno
 * Date: 03-10-2013
 * Time: 19:49
 */
public class CopySite extends SitesCommand {
    private String title;
    private String summary;
    private String sourceHref;

    @Override
    protected void protectedExecute() throws CommandException {
        try {
            SiteEntry entry = new SiteEntry();

            entry.setTitle(new PlainTextConstruct(title));
            entry.setSummary(new PlainTextConstruct(summary));
            entry.addLink(SitesLink.Rel.SOURCE, Link.Type.ATOM, sourceHref);

            setResult(getService().insert(new URL(getSiteFeedUrl()), entry));

        } catch (IOException | ServiceException e) {
            throw new CommandException(e);
        }
    }

    public CopySite setTitle(String title) {
        this.title = title;
        return this;
    }

    public CopySite setSummary(String summary) {
        this.summary = summary;
        return this;
    }

    public CopySite setSourceHref(String sourceHref) {
        this.sourceHref = sourceHref;
        return this;
    }
}
